package com.ruoyi.labsafety.controller;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.labsafety.domain.LabSafetyCheckIndexItem;
import com.ruoyi.labsafety.service.ILabSafetyCheckIndexItemService;

/**
 * 安全检查指标项Controller
 * 
 * @author ruoyi
 * @date 2022-03-06
 */
@RestController
@RequestMapping("/labsafety/labsafetyCheckIndexItem")
public class LabSafetyCheckIndexItemController extends BaseController {
	@Autowired
	private ILabSafetyCheckIndexItemService labSafetyCheckIndexItemService;

	/**
	 * 查询安全检查指标项列表
	 */
	@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:query')")
	@GetMapping("/list")
	public AjaxResult list(LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		//startPage();
		List<LabSafetyCheckIndexItem> list = labSafetyCheckIndexItemService
				.selectLabSafetyCheckIndexItemList(labSafetyCheckIndexItem);
		return AjaxResult.success(list);
	}

	/**
	 * 导出安全检查指标项列表
	 */
	//@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:export')")
	/*@Log(title = "安全检查指标项", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		List<LabSafetyCheckIndexItem> list = labSafetyCheckIndexItemService
				.selectLabSafetyCheckIndexItemList(labSafetyCheckIndexItem);
		ExcelUtil<LabSafetyCheckIndexItem> util = new ExcelUtil<LabSafetyCheckIndexItem>(LabSafetyCheckIndexItem.class);
		return util.exportExcel(list, "labsafetyCheckIndexItem");
	}*/

	/**
	 * 获取安全检查指标项详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(labSafetyCheckIndexItemService.selectLabSafetyCheckIndexItemById(id));
	}

	/**
	 * 新增安全检查指标项
	 */
	@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:edit')")
	@Log(title = "安全检查指标项", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		return toAjax(labSafetyCheckIndexItemService.insertLabSafetyCheckIndexItem(labSafetyCheckIndexItem));
	}

	/**
	 * 修改安全检查指标项
	 */
	@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:edit')")
	@Log(title = "安全检查指标项", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		return toAjax(labSafetyCheckIndexItemService.updateLabSafetyCheckIndexItem(labSafetyCheckIndexItem));
	}

	/**
	 * 删除安全检查指标项
	 */
	@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:edit')")
	@Log(title = "安全检查指标项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public AjaxResult remove(@PathVariable Long id) {
		return toAjax(labSafetyCheckIndexItemService.deleteLabSafetyCheckIndexItemById(id));
	}
	@PreAuthorize("@ss.hasPermi('labsafety:labsafetyCheckIndexItem:query')")
	@GetMapping("/list/exclude")
	public AjaxResult excludeChild(LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		List<LabSafetyCheckIndexItem> subjects = labSafetyCheckIndexItemService
				.selectLabSafetyCheckIndexItemList(labSafetyCheckIndexItem);
		Iterator<LabSafetyCheckIndexItem> it = subjects.iterator();
		while (it.hasNext()) {
			LabSafetyCheckIndexItem d = (LabSafetyCheckIndexItem) it.next();
			if (d.getId().intValue() == labSafetyCheckIndexItem.getId()
					|| ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), labSafetyCheckIndexItem.getId() + "")) {
				it.remove();
			}
		}
		return AjaxResult.success(subjects);
	}
}
